﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//using AC.ExtendedRenderer.Toolkit;
using ComponentFactory.Krypton.Toolkit;
using System.Drawing.Drawing2D;

namespace LED_Utils.MultitrackControl
{
    public partial class ColorWheelPanel : UserControl
    {
        public ColorWheelPanel()
        {
            InitializeComponent();
//---
            //base.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            //base.SetStyle(ControlStyles.UserPaint, true);
            //base.SetStyle(ControlStyles.DoubleBuffer, true);
            //this.pnlSelectedColor.Visible = false;
            //this.pnlBrightness.Visible = false;
            //this.pnlColor.Visible = false;
            //Rectangle selectedColorRectangle = new Rectangle(this.pnlSelectedColor.Location, this.pnlSelectedColor.Size);
            //Rectangle brightnessRectangle = new Rectangle(this.pnlBrightness.Location, this.pnlBrightness.Size);
            //Rectangle colorRectangle = new Rectangle(this.pnlColor.Location, this.pnlColor.Size);
            //this.myColorWheel = new ColorWheel(colorRectangle, brightnessRectangle, selectedColorRectangle);
            //this.myColorWheel.ColorChanged = (ColorWheel.ColorChangedEventHandler)Delegate.Combine(this.myColorWheel.ColorChanged, new ColorWheel.ColorChangedEventHandler(this.myColorWheel_ColorChanged));
            //this.SetRGB(this.RGB);
            //this.SetHSV(this.HSV);
            base.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            base.SetStyle(ControlStyles.UserPaint, true);
            base.SetStyle(ControlStyles.DoubleBuffer, true);

            Init();

        }

        private void myColorWheel_ColorChanged(object sender, ColorChangedEventArgs e)
        {
            this.RGB = e.RGB;
            this.SetRGB(e.RGB);
            this.SetHSV(e.HSV);
        }

        private void RefreshValue(KryptonNumericUpDown nud, int value)
        {
            if (nud.Value != value)
            {
                nud.Value = value;
                nud.Refresh();
            }
        }
        private bool isInUpdate;

        private void SetHSV(ColorHandler.HSV HSV)
        {
            this.isInUpdate = true;
            //this.RefreshValue(this.nudHue, HSV.Hue);
            //this.RefreshValue(this.nudSaturation, HSV.Saturation);
            //this.RefreshValue(this.nudBrightness, HSV.value);
            this.isInUpdate = false;
        }

        private void SetRGB(ColorHandler.RGB RGB)
        {
            this.isInUpdate = true;
            this.RefreshValue(this.nudRed, RGB.Red);
            this.RefreshValue(this.nudBlue, RGB.Blue);
            this.RefreshValue(this.nudGreen, RGB.Green);
            OnColorChanged();
            this.isInUpdate = false;
            // this.txtWebColor.Text = ColorTranslator.ToHtml(System.Drawing.Color.FromArgb(RGB.Red, RGB.Green, RGB.Blue)).ToString();
        }
        private ColorWheel myColorWheel;
        private ChangeStyle changeType = ChangeStyle.None;
        private ColorHandler.HSV HSV;
        private ColorHandler.RGB RGB;
        private Point selectedPoint;


        public System.Drawing.Color Color
        {
            get
            {
                return Color.FromArgb(RGB.Red, RGB.Green, RGB.Blue); //this.myColorWheel.Color;
            }
            set
            {
                if (value.ToArgb() != Color.ToArgb())
                {
                    this.changeType = ChangeStyle.RGB;
                    this.RGB = new ColorHandler.RGB(value.R, value.G, value.B);
                    this.HSV = ColorHandler.RGBtoHSV(this.RGB);
                    base.Invalidate();
                    SetRGB(RGB);

                }
            }
        }

        public event EventHandler  ColorChanged;
        protected virtual void OnColorChanged()
        {
            if (ColorChanged != null)
            {
                ColorChanged(this, EventArgs.Empty);
            }
        }


        private enum ChangeStyle
        {
            MouseMove,
            RGB,
            HSV,
            None
        }

        private void ColorChooser1_Load(object sender, EventArgs e)
        {
           // Init();
        }

        private void Init()
        {
            this.pnlSelectedColor.Visible = false;
            this.pnlBrightness.Visible = false;
            this.pnlColor.Visible = false;
            Rectangle selectedColorRectangle = new Rectangle(this.pnlSelectedColor.Location, this.pnlSelectedColor.Size);
            Rectangle brightnessRectangle = new Rectangle(this.pnlBrightness.Location, this.pnlBrightness.Size);
            Rectangle colorRectangle = new Rectangle(this.pnlColor.Location, this.pnlColor.Size);
            this.myColorWheel = new ColorWheel(colorRectangle, brightnessRectangle, selectedColorRectangle);
            this.myColorWheel.ColorChanged = (ColorWheel.ColorChangedEventHandler)Delegate.Combine(this.myColorWheel.ColorChanged, new ColorWheel.ColorChangedEventHandler(this.myColorWheel_ColorChanged));
            HSV.value = 255;
            this.SetRGB(this.RGB);
            this.SetHSV(this.HSV);
        }

        private void ColorChooser1_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

            switch (this.changeType)
            {
                case ChangeStyle.MouseMove:
                case ChangeStyle.None:
                    this.myColorWheel.Draw(e.Graphics, this.selectedPoint);
                    break;

                case ChangeStyle.RGB:
                    this.myColorWheel.Draw(e.Graphics, this.RGB);
                    break;

                case ChangeStyle.HSV:
                    this.myColorWheel.Draw(e.Graphics, this.HSV);
                    break;
            }
        }
        private void frmMain_MouseUp(object sender, MouseEventArgs e)
        {
            this.myColorWheel.SetMouseUp();
            this.changeType = ChangeStyle.None;
        }

        private void HandleHSVChange(object sender, EventArgs e)
        {
            if (!this.isInUpdate)
            {
                this.changeType = ChangeStyle.HSV;
                // this.HSV = new ColorHandler.HSV((int)this.nudHue.Value, (int)this.nudSaturation.Value, (int)this.nudBrightness.Value);
                this.SetRGB(ColorHandler.HSVtoRGB(this.HSV));
                base.Invalidate();
            }
        }

        private void HandleMouse(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.changeType = ChangeStyle.MouseMove;
                this.selectedPoint = new Point(e.X, e.Y);
                base.Invalidate();
            
            }
           else
            {
                this.changeType = ChangeStyle.None;
            }
        }

        private void HandleRGBChange(object sender, EventArgs e)
        {
            if (!this.isInUpdate)
            {
                this.changeType = ChangeStyle.RGB;
                this.RGB = new ColorHandler.RGB((int)this.nudRed.Value, (int)this.nudGreen.Value, (int)this.nudBlue.Value);
                this.SetHSV(ColorHandler.RGBtoHSV(this.RGB));
                base.Invalidate();
            }
        }


        private void HandleTextChanged(object sender, EventArgs e)
        {
            decimal decimal1 = ((NumericUpDown)sender).Value;
        }

        public override Font Font
        {
            get
            {
                return base.Font  ;
            }
            set
            {
                base.Font =value ;
                Label1.Font = value;
                Label2.Font = value;
                Label3.Font = value;

            }
        }

        private void panel1_Click(object sender, EventArgs e)
        {
            Color = ((Panel)sender).BackColor;
           // HandleColorChange();
        }

    }
}
